package com.staryea.hive;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class AbstractDAO {

    public <T> List<T>  queryList(String sql,ResultSetConvert resultSetConvert) {
        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;
        List result = null;
        try{
            connection = HiveConnectFactory.getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()) {
                if(result == null) {
                    result = new ArrayList<>();
                }
                Object object = resultSetConvert.convertData(resultSet);
                if(object != null) {
                    result.add(object);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if (resultSet != null) {
                    resultSet.close();
                }
                if (statement != null) {
                    statement.close();
                }
                if (connection != null) {
                    connection.close();
                }
            }catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
}
